package main

import (
	"context"
	"fmt"
	_ "go-api/docs"
	"go-api/http"
	httpSvr "go-api/http"
	"go-api/internal/config"
	"go-api/pkg/logger"
	"os"
	"os/signal"
	"syscall"
	"time"
)

// todo 初始化信息
func init() {
	err := config.Load()
	if err != nil {
		logger.Panic(err)
	}

	// 初始化mysql 连接
	err = config.InitDB()
	if err != nil {
		logger.Panic(err)
	}

	err = config.InitRedis()
	if err != nil {
		logger.Panic(err)
	}

	logger.Println("init success!!!")
}

func main() {
	logger.Println("server start...")
	go func() {
		if err := httpSvr.Serve(); err != nil {
			return
		}
	}()

	stop := make(chan bool, 1)
	sigChannel := make(chan os.Signal, 1)
	signal.Notify(sigChannel, syscall.SIGTERM)
	<-sigChannel
	stop <- true
	fmt.Println("关闭")
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	if err := http.Stop(ctx); err != nil {
		logger.Fatalf("server shutdown:%v", err)
	}
}
